Repository navigation
Conversation
out_of_gas/new_epoch without clobbering registers
|
Thanks @0xbe7a for your contribution to this repository, but this automated bot is closing this PR. Bytecode Alliance projects have been overwhelmed with AI-generated PRs that monopolize reviewer time, and this bot is an attempt to push back. Please discuss this change on zulip with maintainers first before jumping straight into making a PR. After discussion, a maintainer may reopen this PR. AI usage must abide by the Bytecode Alliance's AI Tool Use Policy, and we ask that new contributors engage with maintainers beforehand as humans to ensure that what you're working on is reasonable for a newcomer to tackle. For more information on this bot, see the contributing docs. If you believe this PR was caught by this bot erroneously, reach out to us on zulip. |
|
/vouch seems like a reasonable feature, implementation, and presented well enough, so reopening. |
|
/vouch Author has talked with us on Zulip, and I'm willing to review. |
|
(oh, I missed Alex doing the same -- in any case, halfway through reviewing now) |
cfallin
left a comment
There was a problem hiding this comment.
Thanks!
I'll echo what Alex said on Zulip that it'd be great to see a Sightglass run for both fuel vs baseline and epochs vs baseline, before and after this change; e.g. the pca-20-min.suite suite. That's our canonical way of measuring perf of Wasm execution.
| /// Runs `live_values_across_loop_header_wat` with an epoch deadline that is | ||
| /// always reached, so every loop header calls `new_epoch` and returns into | ||
| /// the loop with all the live values; the checksum must not change. | ||
| async fn epoch_preserves_values_live_across_loop_header( |
There was a problem hiding this comment.
I appreciate the intention here but I think the existing epoch tests should be fine to catch any issues (along with all other uses of preserve_all -- you're basically just testing that the ABI works properly), so we can probably remove both this and the below tests, and the new tests in fuel.rs too (which are... impressive... with the inline assembly to actually clobber all, but again redundant wrt our existing runs).
| /// `PreserveAll` trampoline clobbers nothing, so values stay in whatever | ||
| /// registers they are in. The trampoline returns nothing and raises the | ||
| /// builtin's trap itself. | ||
| pub(crate) fn out_of_gas_preserve_all(&mut self, func: &mut Function) -> ir::FuncRef { |
There was a problem hiding this comment.
Is there any reason that we add new libcalls with _preserve_all suffixes, rather than just changing the ABI for the libcall (as we do for breakpoint)?
| Abi::Patchable => unreachable!( | ||
| "We should not be compiling a patchable-ABI trampoline for a component function" | ||
| Abi::PreserveAll => unreachable!( | ||
| "We should not be compiling a preserve_all-ABI trampoline for a component function" |
There was a problem hiding this comment.
Refactorings like this FWIW are generally fine to include in PRs, but where possible we prefer them to be separated out commit-wise to make review easier. No problem this time, but if you're interested in making future PRs we'd appreciate that.
| /// `PreserveAll` trampoline clobbers nothing, so values stay in whatever | ||
| /// registers they are in. The trampoline returns nothing and raises the | ||
| /// builtin's trap itself. | ||
| pub(crate) fn out_of_gas_preserve_all(&mut self, func: &mut Function) -> ir::FuncRef { |
There was a problem hiding this comment.
@cfallin question for you, did you have a particular reason for doing this style of getting the breakpoint intrisnic vs an alternative approach along the lines of https://github.com/bytecodealliance/wasmtime/pull/14975/changes#diff-f4e65f6eab2ed0d2568b98f7090476dab5cf37bbf7e7d5ef10a6a2ca9e97c33e where internally libcalls are special-cased?
Personally I'm partial to the latter approach as it makes so all usage of the libcall is guaranteed to have a single ABI as opposed to accidentally using the wrong helper to get the wrong ABI. Even more ideally I'd love like a #[abi_preserve_all] on the builtins macro itself so we can just annotate some libcalls over others, but that might be a bit overkill to parse when it's only needed in one location here.
There was a problem hiding this comment.
It's ok to skip the tests in this file. These aren't erally adding much and the tests themselves are pretty complicated
There was a problem hiding this comment.
Similar to the previous file, the tests here are pretty overkill to go so far as to use inline assembly to forcibly clobber things. I think it's ok to primarily rely on preexisting functional tests and disas/*.wat for this change
|
(er sorry for the race!) |
Fuel and epoch checks currently call
out_of_gasandnew_epochthrough the regulartail-ABI builtin trampoline. Even though those calls are almost never taken, they sit at every function entry and loop header, which forces regalloc to treat all caller-saved registers as clobbered and spill loop-carried values on every iteration.This PR routes both calls through the
preserve_allbuiltin trampoline (previously only used by breakpoints) and marks theout_of_gasblock cold. Becausepreserve_allcalls can't return values,epoch_check_fullnow reloadsepoch_deadlinefromVMStoreContextafter the call instead of usingnew_epoch's return value (new_epochalready updates that field; Winch is untouched and still uses the regular ABI and return value).Since this trampoline is no longer breakpoint-specific, I renamed
PatchableToBuiltinTrampoline/Abi::PatchabletoPreserveAllToBuiltinTrampoline/Abi::PreserveAllwhile keeping the same encodings. Patchability is still controlled per call site viaExtFuncData::patchable, so only breakpoint sites get NOP'd. Happy to split the rename into a separate PR if preferred. A couple of small cleanups came along with this: builtin trampolines are now deduplicated by FuncKey instead of builtin index, since one builtin can now have two trampolines (normal and preserve_all), and the call-site signature marks vmctx asArgumentPurpose::VMContextto match the callee.Everything around traps, safepoints, and async yields works the same as it does for breakpoints today: all backends already implement
preserve_all(including saving full 128-bit vector registers on aarch64), and when yielding to the host the saved registers live in the trampoline frame on the fiber stack until resumption.Benchmarks
We hit this using
consume_fuelas a compute limit for libjpeg-turbo and PDFium, where fuel overhead was much higher than the counter updates alone explained, especially on aarch64. Measured with the equivalent change on Wasmtime 51 (same fuel/epoch codegen asmain), relative to a no-fuel build:In libjpeg-turbo's hottest decode loop, stack loads per iteration drop from 88 to 5 on x86-64 and from 13 to 2 on aarch64. aarch64 sees a bigger win because Zen 5 hides much of the spill latency, whereas call-heavy code benefits less since we still store and reload the fuel counter around calls.
Testing
Existing fuel and epoch tests keep very little live across loop headers, so they wouldn't catch a broken
preserve_allprologue on the returning paths (fuel refuel or epochContinue/Yield). I added three tests covering fuel yields, epochContinue(0), and epochYield(0)at every loop header with 16i64, 16v128, and 8f64values live across the header, checking the result against an uninstrumented run. Because host code between yield and resume doesn't reliably touch all of those registers, the tests also clobber every caller-saved integer and vector register via inline asm on x86-64 and aarch64 before resuming and inside the epoch callback. As a sanity check, skipping vector saves inpreserve_allon x86-64 makes all three new tests fail while all 144 existing fuel/epoch/debug tests still pass (and without the host-side clobbering, the fuel test missed it too).I also added disas tests (
{fuel,epoch}-loop-live-values-{x64,aarch64}.wat) to pin the spill reduction—e.g. the aarch64 epoch loop goes from 26 loads and 11 stores down to 10 and 1—and updated existing fuel/epoch expectations for the new signature, cold block, and deadline reload.Same approach as #14975 which was closed without technical review
Related #4109